// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-08 15:31
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.TREE;

import com.DataStruct.Tree.TwoBinaryTree.TreeNode;

public class IncreasingBST {
    /**
     * 将给定的二叉搜索树转化为只有右节点的单边二叉树
     */
    TreeNode<Integer> node;
    TreeNode<Integer> tmp;

    public TreeNode increasingBST(TreeNode root) {
        // 通过二叉树的中序遍历实现
        tmp = new TreeNode(root.val);
        node = tmp;
        midSearch(root);
        return node.right;
    }

    /**
     * 中序遍历二叉树的同时实现二叉树的创建
     *
     * @param root 根节点
     */
    private void midSearch(TreeNode root) {
        if (root == null) {
            return;
        }
        if (root.left != null) {
            midSearch(root.left);
        }
        tmp.right = new TreeNode<>(root.val);
        tmp = tmp.right;
        if (root.right != null) {
            midSearch(root.right);
        }
    }
}
